/**
 * 
 */
package com.pmsco.pms.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

import com.pmsco.pms.ajaxdata.MenuListJsonData;
import com.pmsco.pms.ajaxdata.PmsMenuJsonData;
import com.pmsco.pms.annotation.LogonRequired;
import com.pmsco.pms.annotation.PermissionRequired;
import com.pmsco.pms.business.PmsMenuBusiness;
import com.pmsco.pms.business.dto.PmsMenuRequestDTO;
import com.pmsco.pms.business.dto.PmsMenuReturnDTO;
import com.pmsco.pms.core.constant.PmsConstant;
import com.pmsco.pms.core.domain.PmsMenu;
import com.pmsco.pms.util.PmsLogonRequired;
import com.pmsco.pms.util.PmsPermissionRequired;

/**
 * @author Long Tran
 *
 */
@Controller
public class PmsMenuController extends AbstractPmsController {
	private static final Logger logger = LoggerFactory.getLogger(PmsMenuController.class);

	/**
	 * List PMS Menu
	 */
	@RequestMapping(value = PmsConstant.listMenu_Url)
	@LogonRequired(required = PmsLogonRequired.ADMIN_USER)
	@PermissionRequired(required = PmsPermissionRequired.PMS_LIST_MENU_FUNCTION)
	public String listMenu(Locale locale, Model model) {
		logger.info("List Menu Action");
		return PmsConstant.listMenu_View;
	}
	
	/**
	 * List PMS Menu Data
	 */
	@RequestMapping(value = PmsConstant.listMenuData_Url)
	@LogonRequired(required = PmsLogonRequired.ADMIN_USER)
	@PermissionRequired(required = PmsPermissionRequired.PMS_LIST_MENU_FUNCTION)
	public @ResponseBody MenuListJsonData listMenuData(HttpServletRequest request, Locale locale, Model model) {
		logger.info("Begin List Menu Data Action");
		PmsMenuBusiness pmsMenuBusiness = new PmsMenuBusiness();
		
		logger.debug("Build list menu request dto");
		PmsMenuRequestDTO pmsMenuRequestDTO = new PmsMenuRequestDTO();
		Integer page = Integer.parseInt(request.getParameter("page"));
		Integer rows = Integer.parseInt(request.getParameter("rows"));
		String sidx = request.getParameter("sidx");
		String sord = request.getParameter("sord");
		String searchField = request.getParameter("searchField");
		String searchString = request.getParameter("searchString");
		String searchOper = request.getParameter("searchOper");
		if(page == null)
			page = 1;
		if(rows == null)
			rows = 10;
		pmsMenuRequestDTO.setPage(page);
		pmsMenuRequestDTO.setRows(rows);
		pmsMenuRequestDTO.setSidx(sidx);
		pmsMenuRequestDTO.setSord(sord);
		pmsMenuRequestDTO.setSearchField(searchField);
		pmsMenuRequestDTO.setSearchString(searchString);
		pmsMenuRequestDTO.setSearchOper(searchOper);
		
		logger.debug("Call to pms menu business for list menu");
		PmsMenuReturnDTO pmsMenuReturnDTO = pmsMenuBusiness.listMenuData(pmsMenuRequestDTO);
		
		logger.debug("Analysis list menu return dto");
		MenuListJsonData menuListJsonData = new MenuListJsonData();
		Integer records = pmsMenuReturnDTO.getMenulistSize();
		Integer total = (records / rows);
		if(records % rows > 0)
			total += 1;
		menuListJsonData.setPage(page);
		menuListJsonData.setRecords(records);
		menuListJsonData.setTotal(total);
		List<PmsMenuJsonData> data = new ArrayList<PmsMenuJsonData>();
		for(PmsMenu menu : pmsMenuReturnDTO.getMenulist()) {
			data.add(new PmsMenuJsonData(menu));
		}
		menuListJsonData.setRows(data);
		
		logger.info("End List Menu Data Action");
		return menuListJsonData;
	}
	
	/**
	 * Edit PMS Menu
	 */
	@RequestMapping(value = PmsConstant.editMenu_Url)
	@LogonRequired(required = PmsLogonRequired.ADMIN_USER)
	@PermissionRequired(required = PmsPermissionRequired.PMS_EDIT_MENU_FUNCTION)
	public ModelAndView editMenu(@RequestParam(value = "menuName") String menuName) {
		logger.info("Begin Edit Menu Action");
		PmsMenuBusiness pmsMenuBusiness = new PmsMenuBusiness();
		
		logger.debug("Build edit menu request dto");
		PmsMenuRequestDTO pmsMenuRequestDTO = new PmsMenuRequestDTO();
		pmsMenuRequestDTO.setMenuName(menuName);
		
		logger.debug("Call to pms menu business for edit menu");
		PmsMenuReturnDTO pmsMenuReturnDTO = pmsMenuBusiness.editMenu(pmsMenuRequestDTO);
		
		logger.debug("Analysis create menu return dto");
		ModelAndView ret = new ModelAndView(pmsMenuReturnDTO.getReturnView(), "command", pmsMenuReturnDTO.getPmsMenu());
		ret.addObject(PmsConstant.parentMenuList, pmsMenuReturnDTO.getMenulist());
		ret.addObject(PmsConstant.roleList, pmsMenuReturnDTO.getRolelist());
		ret.addObject(PmsConstant.roleEnableList, pmsMenuReturnDTO.getMenuRoleList());
		
		logger.info("End Edit Menu Action");
		return ret;
	}
	
	/**
	 * Create PMS Menu
	 */
	@RequestMapping(value = PmsConstant.createMenu_Url)
	@LogonRequired(required = PmsLogonRequired.ADMIN_USER)
	@PermissionRequired(required = PmsPermissionRequired.PMS_CREATE_MENU_FUNCTION)
	public ModelAndView createMenu(Locale locale, Model model) {
		logger.info("Begin Create Menu Action");
		PmsMenuBusiness pmsMenuBusiness = new PmsMenuBusiness();
		
		logger.debug("Call to pms menu business for create menu");
		PmsMenuReturnDTO pmsMenuReturnDTO = pmsMenuBusiness.createMenu(new PmsMenuRequestDTO());
		
		logger.debug("Analysis create menu return dto");
		ModelAndView ret = new ModelAndView(pmsMenuReturnDTO.getReturnView(), "command", pmsMenuReturnDTO.getPmsMenu());
		ret.addObject(PmsConstant.parentMenuList, pmsMenuReturnDTO.getMenulist());
		ret.addObject(PmsConstant.roleList, pmsMenuReturnDTO.getRolelist());
		ret.addObject(PmsConstant.roleEnableList, pmsMenuReturnDTO.getMenuRoleList());
		
		logger.info("End Create Menu Action");
		return ret;
	}
	
	/**
	 * Save PMS Menu
	 */
	@RequestMapping(value = PmsConstant.saveMenu_Url)
	@LogonRequired(required = PmsLogonRequired.ADMIN_USER)
	@PermissionRequired(required = PmsPermissionRequired.PMS_SAVE_MENU_FUNCTION)
	public ModelAndView saveMenu(@ModelAttribute("menuDetail") PmsMenu menuDetail, Locale locale, Model model, HttpServletRequest request) {
		logger.info("Begin Save Menu Action");
		PmsMenuBusiness pmsMenuBusiness = new PmsMenuBusiness();
		
		logger.debug("Build save menu request dto");
		PmsMenuRequestDTO pmsMenuRequestDTO = new PmsMenuRequestDTO();
		
		if(menuDetail.getUrl() == null)
			menuDetail.setUrl("");
		
		if("on".equalsIgnoreCase(menuDetail.getIsLogonRequired()))
			menuDetail.setIsLogonRequired("T");
		else 
			menuDetail.setIsLogonRequired("F");
		
		if("on".equalsIgnoreCase(menuDetail.getIsTopLevel()))
			menuDetail.setIsTopLevel("T");
		else 
			menuDetail.setIsTopLevel("F");
		
		Long parentId = Long.valueOf(request.getParameter("parentId"));
		if(parentId.intValue() > 0) {
			pmsMenuRequestDTO.setParentId(parentId);
		}
		
		Long oldParentId = null;
		if(request.getParameter("oldParentId") != null && !request.getParameter("oldParentId").trim().isEmpty())
			oldParentId = Long.valueOf(request.getParameter("oldParentId"));
		pmsMenuRequestDTO.setOldParentId(oldParentId);
		
		Long orderId = new Long(0);
		if(request.getParameter("orderId") != null && !request.getParameter("orderId").trim().isEmpty())
			orderId = Long.valueOf(request.getParameter("orderId"));
		pmsMenuRequestDTO.setOrderId(orderId);
		pmsMenuRequestDTO.setPmsMenu(menuDetail);
		
		logger.debug("Get enable menu and do save");
		String[] enableMenuIds = request.getParameterValues("enableRole");
		pmsMenuRequestDTO.setEnableMenuIds(enableMenuIds);
		
		logger.debug("Call to pms menu business for save menu");
		PmsMenuReturnDTO pmsMenuReturnDTO = pmsMenuBusiness.saveMenu(pmsMenuRequestDTO);
		
		logger.debug("Analysis save menu return dto");
		ModelAndView ret = null;
		if(pmsMenuReturnDTO.getReturnUrl() != null && !pmsMenuReturnDTO.getReturnUrl().isEmpty()) {
			ret = new ModelAndView(new RedirectView(pmsMenuReturnDTO.getReturnUrl()));
		} else {
			ret = new ModelAndView(pmsMenuReturnDTO.getReturnView(), "command", pmsMenuReturnDTO.getPmsMenu());
			ret.addObject(PmsConstant.errorMessage, pmsMenuReturnDTO.getErrorMessage());
			ret.addObject(PmsConstant.parentMenuList, pmsMenuReturnDTO.getMenulist());
			ret.addObject(PmsConstant.roleList, pmsMenuReturnDTO.getRolelist());
			ret.addObject(PmsConstant.roleEnableList, pmsMenuReturnDTO.getMenuRoleList());
		}
		logger.info("End Save Menu Action");
		return ret;
	}
	
	/**
	 * Delete menu
	 */
	@RequestMapping(value = PmsConstant.deleteMenu_Url)
	@LogonRequired(required = PmsLogonRequired.ADMIN_USER)
	@PermissionRequired(required = PmsPermissionRequired.PMS_DELETE_MENU_FUNCTION)
	public String deleteMenu(Locale locale, Model model, HttpServletRequest request) {
		PmsMenuBusiness pmsMenuBusiness = new PmsMenuBusiness();
		logger.info("Begin Delete Menu Action");
		String deleteMenuNames = request.getParameter("selectedMenuNames");
		pmsMenuBusiness.deleteMenus(deleteMenuNames);
		logger.info("End Delete Menu Action");
		return PmsConstant.listMenu_View;
	}
}
